package com.itheima.controller;


import com.itheima.anno.LogOperation;
import com.itheima.pojo.Dept;
import com.itheima.pojo.Result;
import com.itheima.service.DeptService;
import com.itheima.service.impl.DeptServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@Slf4j
@RequestMapping("/depts")
@RestController
public class DeptController {
//    private static final Logger log = LoggerFactory.getLogger(DeptController.class);

    @Autowired
    private DeptService deptService;

//    private DeptService deptService = new DeptServiceImpl();

    /*
    * 使用RequestMapping的衍生注解
    * @GetMapping 该注解是标识当前方法，必须以GET方式请求*/
    @LogOperation
    @GetMapping
    public Result list(){
//        System.out.println("查询部门列表");
        log.info("查询部门列表");
        List<Dept> deptList = deptService.findAll();
        return Result.success(deptList);
    }

    @LogOperation
    @DeleteMapping
    public Result delete(Integer id){
//        System.out.println("根据id删除部门：" + id);
        log.info("根据id删除部门, id:{}" + id);
        deptService.deleteById(id);
        return Result.success();
    }

    /*
    * 新增部门--POST http://localhost:8080/depts
    * 请求参数： {“name”：“研发部”}*/

    @LogOperation
    @PostMapping
    public Result save(@RequestBody Dept dept){
//        System.out.println("新增部门：" + dept);
        log.info("新增部门,dept:{}", dept);
        deptService.save(dept);
        return Result.success();
    }

    //查询回显
    /*
    * 根据id查询： GET http://localhost:8080/depts/1*/

    @LogOperation
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id){
//        System.out.println("根据id查询：" + id);
        log.info("根据id查询, id:{}", id);
        Dept dept = deptService.getById(id);
        return Result.success(dept);
    }

    /**
     * 修改部门 - PUT http://localhost:8080/depts
     * 请求参数：{"id":1,"name":"研发部"}
     */
    @LogOperation
    @PutMapping
    public Result update(@RequestBody Dept dept){
//        System.out.println("修改部门：" + dept);
        log.info("修改部门,dept:{}", dept);
        deptService.update(dept);
        return Result.success();
    }
}
